package com.eianiao.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eianiao.bean.Setmeal;
import com.eianiao.common.PageParam;
import com.eianiao.common.R;
import com.eianiao.dto.SetmealDto;
import com.eianiao.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author eianiao
 * @Date 2022 06
 * @Version 1.0
 **/
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;


    /**
     * 添加一套套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R add(@RequestBody SetmealDto setmealDto){
        int row = setmealService.add(setmealDto);
        if (row >0){
            return R.success("添加套餐成功！");
        }
        return R.error("添加套餐失败！");
    }


    /**
     * 分页查询
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R findPage(PageParam pageParam){

        try {
            IPage<SetmealDto> dtoIPage = setmealService.findPage(pageParam);

            return R.success(dtoIPage);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("分页查询失败！");
        }
    }


    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){
        int row = setmealService.deleteById(ids);
        if (row >0){
            return R.success("删除套餐成功！");
        }
        return R.error("删除套餐失败！");
    }

    /**
     * 更改套餐的状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R updateByStatus(@PathVariable int status,@RequestParam List<Long> ids){
        int row  = setmealService.updateByStatus(status,ids);
        if (row >0){
            return R.success("更新套餐状态成功！");
        }
        return R.error("更新套餐状态失败！");

    }

    /**
     * 回显套餐数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R findById(@PathVariable Long id){
        try {
            SetmealDto setmealDto = setmealService.findById(id);
            return R.success(setmealDto);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("回显数据失败");
        }
    }

    /**
     * 更新套餐
     * @return
     */
    @PutMapping
    public R update(@RequestBody SetmealDto setmealDto){
        int row = setmealService.update(setmealDto);
        if (row >0){
            return R.success("更新套餐成功！");
        }
        return R.error("更新套餐失败！");

    }

//移动端---------------------------

    /**
     * 根据分类来获取该分类下的套餐数据
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    public R list(Long categoryId , Integer status){

        List<Setmeal> list = setmealService.findByCategoryId(categoryId, status);
        return R.success(list);

    }
}
